package com.hktf.uilib.utils;

import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.Config;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.SharePreUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description: 判断是否超过30天的utils
 * author: hexunwu
 * date: 2017/7/12 15:42.
 */

public class CheckTimeUtil {
    private static int legalDays = 15;//超时时间
    private static int tipDays = 10;//即将超时提示时间

    public enum CheckTimeType {
        /**
         * LEGALTYPE超时判断
         * TIPTYPE即将超时，显示提示
         */
        LEGALTYPE, TIPTYPE;
    }

    /**
     * 根据不同的项目初始化超时时间
     */
    public static void init() {
        if ("YDS-C60-Android".equals(Config.CLIENTNAME)) {
            legalDays = 15;
            tipDays = 10;
        } else {
            legalDays = 30;
            tipDays = 25;
        }
    }

    /**
     * 判断是否超过了10天或者15天
     * type=LEGALTYPE超时判断  type=TIPTYPE即将超时，显示提示
     */
    public static boolean isBeyondLegalTimeNew(CheckTimeType type) {
        long leadDays = 0;
        long leadHours = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = format.format(new Date());
        //        currentTime="2020-12-15 15:20:30";
        String permissionTime = SharePreUtils.getUseTime(CommUtils.getContext(), SharePreUtils.PERMISSIONTIME, "");
        String lastRunTime = SharePreUtils.getUseTime(CommUtils.getContext(), SharePreUtils.LASTRUNTIME, "");
        LogMds.i("disableuse", "permissionTime" + permissionTime);
        LogMds.i("disableuse", "lastRunTime" + lastRunTime);

        // 当前时间+12小时<LastRunTime，认为用户修改了系统时间，认为超过30天
        try {
            Date currentDate = format.parse(currentTime);
            Date lastRunDate = format.parse(lastRunTime);
            long diff = lastRunDate.getTime() - currentDate.getTime();
            leadHours = diff / (1000 * 60 * 60);
            LogMds.i("cdz", "currentDate=" + currentTime);
            LogMds.i("cdz", "lastRunDate=" + format.format(lastRunDate));
            LogMds.w("cdz", "diff=" + diff);
            LogMds.w("cdz", "leadHours=" + leadHours);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (leadHours > 12) {
            if (type == CheckTimeType.TIPTYPE) {
                return false;
            } else {
                return true;
            }
        }
        // 当前时间>PermissionTime+30天，认为30天没联网，同时设置LastRunTime为当前时间
        try {
            Date currentDate = format.parse(currentTime);
            Date permissionDate = format.parse(permissionTime);
            long diff = currentDate.getTime() - permissionDate.getTime();
            leadDays = diff / (1000 * 60 * 60 * 24);
            LogMds.i("disableuse", "leadDays" + leadDays);
            LogMds.i("cdz", "currentDate=" + currentTime);
            LogMds.i("cdz", "permissionDate=" + format.format(permissionDate));
            LogMds.w("cdz", "diff=" + diff);
            LogMds.w("cdz", "leadDays=" + leadDays);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (type == CheckTimeType.TIPTYPE) {
            if (leadDays > tipDays && leadDays <= legalDays) {
                //弹出禁用提示框
                SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String limitDate = formatDate.format(new Date());
                SharePreUtils.setUseTime(CommUtils.getContext(), SharePreUtils.LASTRUNTIME, limitDate);
                return true;
            }
        } else {
            if (leadDays > legalDays) {
                //弹出禁用提示框
                SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String limitDate = formatDate.format(new Date());
                SharePreUtils.setUseTime(CommUtils.getContext(), SharePreUtils.LASTRUNTIME, limitDate);
                return true;
            }
        }

        return false;
    }

    /**
     * @return 距离被限制使用还有多少天
     */
    public int distanceLimitDays() {
        long leadDays = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = format.format(new Date());
        String permissionTime = SharePreUtils.getUseTime(CommUtils.getContext(), SharePreUtils.PERMISSIONTIME, "");

        // 当前时间>PermissionTime+30天，认为30天没联网，同时设置LastRunTime为当前时间
        try {
            Date currentDate = format.parse(currentTime);
            Date permissionDate = format.parse(permissionTime);
            long diff = currentDate.getTime() - permissionDate.getTime();
            leadDays = diff / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (leadDays > tipDays) {
            //弹出禁用提示框
            //return (legalDays + 1 - (int) leadDays);
            return ((int) leadDays);
        }
        return 0;
    }

}
